const express = require("express");
const app = express()
    .post("/gitee", express.json(), function (req, res) {
        if (!req.body || !req.body.password || !req.body.project || !req.body.project.git_http_url) {
            res.status(400).send("Format incorrect.");
            return;
        }
        const git = req.body.project.git_http_url;
        const password = req.body.password;
        const db = require("./db");
        db.getChatId(git, password).then(function (chat) {
            if (!chat) {
                res.status(400).send("Repository is not registered.");
                return;
            }
            if (req.body.hook_name === "push_hooks") {
                require("./tgService").push(chat.chatId, {
                    user: {
                        name: req.body.sender.username,
                        url: req.body.sender.url
                    },
                    commitCount: req.body.total_commits_count,
                    commits: req.body.commits,
                    repository: {
                        name: req.body.repository.name,
                        url: req.body.repository.url
                    }
                });
            }
            res.sendStatus(200);
        });
    });
module.exports = app;